using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame.MapComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CloudComponent : Microsoft.Xna.Framework.GameComponent
    {
        Texture2D sprite;
        Rectangle spriteRectangle;
        Vector2 position;
        float speed;

        public CloudComponent(Game game, Vector2 startPosition, Texture2D sprite, double speed)
            : base(game)
        {
            this.position.X = startPosition.X;
            this.position.Y = startPosition.Y;

            this.sprite = sprite;

            this.speed = (float)speed;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            spriteRectangle.Width = sprite.Width;
            spriteRectangle.Height = sprite.Height;

            //Bump up speed to avoid jerky movements
            if (speed < 0.4)
                speed = 0.4f;

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime, int index, Camera2DComponent camera, CloudSpawnerComponent spawner)
        {
            position.X -= speed;

            //Move
            spriteRectangle.X = (int)position.X;
            spriteRectangle.Y = (int)position.Y;

            //Despawn if "behind" camera
            if (position.X < camera.getPosition().X - sprite.Width - 10)
                spawner.Despawn(index);

            base.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //Spritebatch allready began in map component
            spriteBatch.Draw(sprite, spriteRectangle, Color.White);
        }
    }
}
